//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package org.jim.server.processor.chat;

import java.util.Iterator;
import java.util.List;
import org.jim.core.ImChannelContext;
import org.jim.core.config.ImConfig.Global;
import org.jim.core.message.MessageHelper;
import org.jim.core.packets.ChatBody;
import org.jim.core.packets.ChatType;
import org.jim.core.packets.Message;
import org.jim.server.config.ImServerConfig;
import org.jim.server.processor.SingleProtocolCmdProcessor;
import org.jim.server.util.ChatKit;

public abstract class BaseAsyncChatMessageProcessor implements SingleProtocolCmdProcessor {
	protected ImServerConfig imServerConfig = (ImServerConfig)Global.get();

	public BaseAsyncChatMessageProcessor() {
	}

	protected abstract void doProcess(ChatBody var1, ImChannelContext var2);

	public void process(ImChannelContext imChannelContext, Message message) {
		ChatBody chatBody = (ChatBody)message;
		boolean isStore = ImServerConfig.ON.equals(this.imServerConfig.getIsStore());
		if (isStore) {
			if (ChatType.CHAT_TYPE_PUBLIC.getNumber() == chatBody.getChatType()) {
				this.pushGroupMessages("push", "store", chatBody, isStore);
			} else {
				String fromId = chatBody.getFromId();
				String toId = chatBody.getToId();
				String sessionId = ChatKit.sessionId(fromId, toId);
				this.writeMessage("store", "user:" + sessionId, chatBody);
				this.writeMessage("push", "user:" + toId + ":" + fromId, chatBody);
			}
		}

		this.doProcess(chatBody, imChannelContext);
	}

	private void pushGroupMessages(String pushTable, String storeTable, ChatBody chatBody, boolean isStore) {
		MessageHelper messageHelper = this.imServerConfig.getMessageHelper();
		String groupId = chatBody.getGroupId();
		this.writeMessage(storeTable, "group:" + groupId, chatBody);
		List<String> userIds = messageHelper.getGroupUsers(groupId);
		Iterator var8 = userIds.iterator();

		while(var8.hasNext()) {
			String userId = (String)var8.next();
			this.writeMessage(pushTable, "group:" + groupId + ":" + userId, chatBody);
		}

	}

	private void writeMessage(String timelineTable, String timelineId, ChatBody chatBody) {
		MessageHelper messageHelper = this.imServerConfig.getMessageHelper();
		messageHelper.writeMessage(timelineTable, timelineId, chatBody);
	}
}
